package com.li.commons.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookiesUtil {

    public static String getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        String str = null;
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie c = cookies[i];
                if (c.getName().equalsIgnoreCase(cookieName)) {
                    str = c.getValue();
                }
            }
        }
        return str;
    }
    
    /**
     * 浏览器关闭cookie失效
     * @param response
     * @param domain
     * @param cookieName
     * @param cookieValue
     */
    public static void setCookie(HttpServletResponse response, String domain,String cookieName, String cookieValue){
    	Cookie cookies = null;
        cookies = new Cookie(cookieName, cookieValue);
        cookies.setPath("/");
        if(domain!=null){
            cookies.setDomain(domain);
        }
        response.addCookie(cookies);
    }

     /**
      * 按天设置cookie有效期
      * @param response
      * @param domain
      * @param cookieName
      * @param cookieValue
      * @param days
      */
    public static void setCookie(HttpServletResponse response, String domain,String cookieName, String cookieValue, Integer days) {
        Cookie cookies = null;
        cookies = new Cookie(cookieName, cookieValue);
        if(days !=null){
            cookies.setMaxAge(60 * 60 * 24 * days);
        }
        cookies.setPath("/");
        if(domain!=null){
            cookies.setDomain(domain);
        }
        response.addCookie(cookies);
    }
    
    /**
     * 按分钟设置cookie有效期
     * @param response
     * @param domain
     * @param cookieName
     * @param cookieValue
     * @param min
     */
    public static void setCookieByMin(HttpServletResponse response, String domain,String cookieName, String cookieValue, Integer min) {
        Cookie cookies = null;
        cookies = new Cookie(cookieName, cookieValue);
        if(min !=null){
            cookies.setMaxAge(60 * min);
        }
        cookies.setPath("/");
        if(domain!=null){
            cookies.setDomain(domain);
        }
        response.addCookie(cookies);
    }

    /**
     * 删除cookie
     * @param response
     * @param domain
     * @param path
     * @param cookieName
     * @param cookieValue
     */
    public static void delCookie(HttpServletResponse response, String domain, String path, String cookieName) {
        Cookie cookies = new Cookie(cookieName, null);
        if(domain!=null){
            cookies.setDomain(domain);
        }
        if(path!=null){
        	cookies.setPath(path);
        }
        cookies.setMaxAge(0);
        response.addCookie(cookies);
    }

    /**
     * 删除所有cookie
     * @param request
     * @param response
     */
    public static void delAllCookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie c = cookies[i];
                c.setValue(null);
                c.setMaxAge(0);
                response.addCookie(c);
            }
        }
    }
}
